//
//  ChargeInvoiceCompanyViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/10.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "ChargeInvoiceCompanyViewController.h"
#import "DecorateAppointMultiTableViewCell.h"
#import "TicketCenterViewTableViewCell.h"
#import "MyOrderDetailViewController.h"

static NSString *CellIDentifier = @"CellIDentifier";
static NSString *UnitIDentifier = @"UnitIDentifier";
static NSString *DefaultCellID = @"DefaultCellID";
@interface ChargeInvoiceCompanyViewController ()<UITableViewDelegate,UITableViewDataSource,DecorateTapSelectTableCellDelegate>

@property (nonatomic,strong)NSArray *keyArray;

@property (nonatomic,strong)NSMutableArray *valueArray;

@property (nonatomic,strong)NSArray *placeholderArr;


@end

@implementation ChargeInvoiceCompanyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self createMainView];
}

- (void)createMainView{
    self.title = @"索取发票";
    AutoWeakSelf
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT);
        make.height.mas_offset(F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0));
    }];
    [self.tableView registerClass:[DecorateAppointInputTextTableCell class] forCellReuseIdentifier:CellIDentifier];
    [self.tableView registerClass:[DecorateAppointSquare2TableCell class] forCellReuseIdentifier:UnitIDentifier];
    [self.tableView registerClass:[TicketDetailDefaultTableCell class] forCellReuseIdentifier:DefaultCellID];
    self.keyArray = @[@[@"企业名称",@"企业税号",@"注册地址",@"电话号码",@"开户银行",@"银行账号"],@[@"发票内容",@"发票金额"],@[@"收件人",@"联系电话",@"收货地址"]];
    self.valueArray = [NSMutableArray array];
    [self.valueArray addObject:[NSMutableArray arrayWithArray:
                                @[self.invoideModel.invoice_company_name?: @"",
                                  self.invoideModel.invoice_no?:@"",
                                  self.invoideModel.invoice_company_address?:@"",
                                  self.invoideModel.invoice_company_phone?:@"",
                                  self.invoideModel.invoice_bank_name?:@"",
                                  self.invoideModel.invoice_bank_no?:@""]]];
    [self.valueArray addObject:[NSMutableArray arrayWithArray:@[@"技术费",[NSString stringWithFormat:@"%@元",self.orderModel.order_price]?:@""]]];
    [self.valueArray addObject:[NSMutableArray arrayWithArray:@[@"",@"",@""]]];
    self.placeholderArr = @[@"请输入收件人",@"请输入联系方式",@"请输入收货地址"];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.keyArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [(NSMutableArray *)self.keyArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 || indexPath.section == 1) {
        TicketDetailDefaultTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DefaultCellID];
        [cell cellForTicketCell];
        cell.nameLabel.text = self.keyArray[indexPath.section][indexPath.row];
        cell.subTitleLabel.text = self.valueArray[indexPath.section][indexPath.row];
        return cell;
    }else{
        DecorateAppointInputTextTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIDentifier];
        cell.delegate = self;
        cell.indexPath = indexPath;
        cell.nameLabel.text = self.keyArray[indexPath.section][indexPath.row];
        cell.textField.text = self.valueArray[indexPath.section][indexPath.row];
        cell.textField.placeholder = self.placeholderArr[indexPath.row];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45.f;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == [self.keyArray count] - 1) {
        return 120;
    }
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 10)];
    backView.backgroundColor = [UIColor appColor];
    return backView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == [self.keyArray count] - 1) {
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 120)];
        backView.backgroundColor = [UIColor appColor];
        UIButton *chargeInvoiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [chargeInvoiceBtn setTitle:@"索取发票" forState:UIControlStateNormal];
        [chargeInvoiceBtn setTitle:@"索取发票" forState:UIControlStateSelected];
        [chargeInvoiceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [chargeInvoiceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [chargeInvoiceBtn setBackgroundColor:[HSPublic colorWithHexString:@"#333333"]];
        chargeInvoiceBtn.layer.cornerRadius = 10;
        chargeInvoiceBtn.layer.masksToBounds = YES;
        [backView addSubview:chargeInvoiceBtn];
        [chargeInvoiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView).offset(15);
            make.top.equalTo(backView).offset(60);
            make.size.mas_equalTo(CGSizeMake(F_DEVICE_W - 30, 50));
        }];
        [chargeInvoiceBtn addTarget:self action:@selector(chargeInvoiceButtonAction) forControlEvents:UIControlEventTouchUpInside];
        return backView;
    }
    return nil;
}

- (void)chargeInvoiceButtonAction{
    BOOL isReturn = NO;
    for (NSInteger i = 3; i < [self.placeholderArr count]; i++) {
        if (k_Is_Empty(self.valueArray[2][i])) {
            [self showErrorTipWithTitle:self.placeholderArr[i]];
            isReturn = YES;
            break;
        }
    }
    if (isReturn) {  return;  }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:K_UserDefault_Read(CustomerUserID)?:@"" forKey:@"member_id"];
    [dict setObject:K_UserDefault_Read(CustomerUserToken)?:@"" forKey:@"member_token"];
    [dict setObject:self.orderModel.order_id?:@"" forKey:@"order_id"];
    [dict setObject:self.invoideModel.invoice_id forKey:@"invoice_id"];
    [dict setObject:self.valueArray[2][0]?:@"" forKey:@"invoice_member"];
    [dict setObject:self.valueArray[2][1]?:@"" forKey:@"invoice_member_phone"];
    [dict setObject:self.valueArray[2][2]?:@"" forKey:@"invoice_member_address"];
    AutoWeakSelf
    [[QYNetManager sharedManager]POST:kInsertMaintailOrderInvoice parameters:dict andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            if ([responseObject[@"data"] integerValue]) {
                [weakSelf showTipWithTitle:@"操作成功"];
                NSArray *subViews = weakSelf.navigationController.viewControllers;
                for (BaseViewController *backVC in subViews) {
                    if ([backVC isKindOfClass:[MyOrderDetailViewController class]]) {
                        backVC.isPop = YES;
                        backVC.popArray = @[self.orderModel.order_id?:@""];
                        [weakSelf.navigationController popToViewController:backVC animated:YES];
                    }
                }
            }else{
                [weakSelf showErrorTipWithTitle:@"操作失败\n请重试"];
            }
        }
    } Failure:^(NSError *error) {
        [weakSelf showErrorTipWithTitle:error.localizedDescription];
    }];
}

#pragma mark - DecorateTapSelectTableCellDelegate
- (void)modifSingleTextFieldTableCell:(NSIndexPath *)indexPath WithEditEndText:(NSString *)text{
    if (k_Is_Empty(text)) {
        return;
    }
    [self.valueArray.lastObject replaceObjectAtIndex:indexPath.row withObject:text];
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:2]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
